package com.snake.springboot.authority.web.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *全局异常处理
 * @author YANG
 */
@Component
@RestControllerAdvice
public class ExceptionAdviceHandler {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionAdviceHandler.class);
    private ApplicationContext applicationContext;
    @Autowired
    public ExceptionAdviceHandler(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<Object> exception(Exception e) {
        logger.error("程序出错：" ,e);
        String msg = e.getMessage()==null?e.toString():e.getMessage();
        return ResponseResultFactory.error(msg);
    }

}
